package com.qmhl.toolposition

import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.widget.Toast
import androidx.annotation.RequiresApi
import com.permissionx.guolindev.PermissionX
import com.qmhl.toolposition.activity.MainActivity
import com.qmhl.toolposition.base.BaseActivity
import com.qmhl.toolposition.databinding.ActivitySplashBinding
import com.qmhl.toolposition.utils.PreferenceUtil

class SplashActivity : BaseActivity() {

    private lateinit var binding: ActivitySplashBinding
    lateinit var pList: List<String>

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivitySplashBinding.inflate(layoutInflater)
        setContentView(binding.root)
        requestPermission()
    }

    private fun requestPermission() {
        pList = mutableListOf<String>()

        // 获取当前Android版本
        val currentVersion = Build.VERSION.SDK_INT

        // 判断当前Android版本并执行相应的操作

        if (currentVersion < Build.VERSION_CODES.Q) {
            // 应用的targetSdkVersion<Q，谷歌提供了兼容性方案，
            // 只要应用申请了老的位置权限ACCESS_FINE_LOCATION或者ACCESS_COARSE_LOCATION，
            // 会默认请求ACCESS_BACKGROUND_LOCATION权限，动态授权弹框参考下面第一个图
            pList = mutableListOf<String>(
                android.Manifest.permission.ACCESS_FINE_LOCATION,
                android.Manifest.permission.READ_PHONE_STATE,
                android.Manifest.permission.ACCESS_NETWORK_STATE,
                android.Manifest.permission.ACCESS_WIFI_STATE,
                android.Manifest.permission.CHANGE_WIFI_STATE,
                android.Manifest.permission.ACCESS_LOCATION_EXTRA_COMMANDS,
                android.Manifest.permission.FOREGROUND_SERVICE,
                android.Manifest.permission.POST_NOTIFICATIONS,
            )


        } else if (currentVersion >= Build.VERSION_CODES.Q) {
            //应用的TargetSdkVersion>=Q，如果应用必须要始终定位，可以只申请ACCESS_BACKGROUND_LOCATION即可，
            // 权限弹框参考下面第三个图；如果应用只需要申请前台定位，则只需要申请老的定位权限即可，
            // 具体授权弹框参考第二个图。如果都申请则出现三态权限弹框，参考下面第一个图。
            pList = mutableListOf<String>(
                android.Manifest.permission.ACCESS_FINE_LOCATION,
                android.Manifest.permission.READ_PHONE_STATE,
                android.Manifest.permission.ACCESS_NETWORK_STATE,
                android.Manifest.permission.ACCESS_WIFI_STATE,
                android.Manifest.permission.CHANGE_WIFI_STATE,
                android.Manifest.permission.ACCESS_LOCATION_EXTRA_COMMANDS,
                android.Manifest.permission.POST_NOTIFICATIONS,

                )
        }

        PermissionX.init(this)
            .permissions(
                pList
            )
            .request { allGranted, grantedList, deniedList ->

                if (allGranted) {
                    binding.root.postDelayed({
                        startMainActivity()
                    }, 1200)
                } else {
                    Toast.makeText(
                        this,
                        "These permissions are denied: $deniedList",
                        Toast.LENGTH_LONG
                    ).show()
                }

            }
    }


    override fun onResume() {
        super.onResume()
//        val currentVersion = Build.VERSION.SDK_INT
//        if (currentVersion >= Build.VERSION_CODES.Q) {
//            var granted =
//                PermissionX.isGranted(this, android.Manifest.permission.ACCESS_BACKGROUND_LOCATION)
//            if (!granted) {
//                requestSpecPermission()
//            }
//        }

    }


    override fun initData() {
        super.initData()
        val text = "verbernbn"


    }

    private fun startMainActivity() {
        val intent = Intent(this, MainActivity::class.java).apply {
            startActivity(this)
            finish()
        }
    }


}